template.defaults.imports.TimestampToTime = function (timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m + s;
};


    var loading = false; //状态标记
    var hasNextPage = false;
    var pageNo = 1;
   
    queryList(pageNo);

    //下拉刷新
    $("#aroundBox").pullToRefresh().on("pull-to-refresh", function () {
        $("#contentTo").html("");
        pageNo = 1;
        queryList(pageNo,function(){
            $("#aroundBox").pullToRefreshDone(); // 重置下拉刷新
        });
    });
  
    //上拉加载更多
    $('#aroundBox').infinite().on("infinite", function () {
        if (hasNextPage){
            pageNo++;
            queryList(pageNo,function(){
            },true);
        }        
    });

    function queryList(pageNo,callback,isMore){
        if (loading) return;
        loading = true;
        var params = {};
        params.start = (pageNo - 1) * 20;
        params.limit = 20;
         /* global getCookie */
         params.token = getCookie("token");
        
         
        $.get('/antique/api/mallAuction/manage/query_list', params, function (result) {
            if (result.code == 0) {
                if (!isMore){
                    /* global template */
                    document.getElementById('contentTo').innerHTML = template('content', result.data);                
                } else {
                    $("#contentTo").append(template('content', result.data));
                }
                hasNextPage = result.data.hasNextPage;
                if (!result.data.hasNextPage) {
                    $('#loadmore').hide();
                    $('#nomore').show();
                } else {
                    $('#loadmore').show();
                    $('#nomore').hide();
                }
                if (callback) callback();
                loading = false;
            }

            
        });
    }
 
    function upSpecial (proId,productHoldCount) {
        // if (productHoldCount == '' || (productHoldCount && productHoldCount < 1)) {
        //     $.toast("专场藏品不能少于10个", "cancel");
        //     return ;
        // }
        $.confirm("您是否确认发布该专场？", "", function() {

            $.post('/antique/api/mallAuction/manage/check_publish_money', { id:proId,token:getCookie('token') }, function (result) {
                if (result.code == 0) {
                    if (result.data.needMoney > 0) {
                        $("#payMoney").html(result.data.needMoney);
                        $("#payId").val(result.data.id);
                        $("#pay_deposit").popup();
                    } else {
                        var params = {};
                        params.id = result.data.id;
                        params.token = getCookie('token');
                        publicSpecial(params);
                    }

                    
                   
                } else {
                 $.toast(result.message, "cancel");
                }
             });
            
          }, function() {
            //取消操作
          });
        
    }
    function delSpecial (proId) {

        $.confirm("您是否确认删除该专场？", "", function() {

            $.post('/antique/api/mallAuction/manage/delete_auction', { id:proId,token:getCookie('token') }, function (result) {
                if (result.code == 0) {
                    $.toast("操作成功！");
                    $("#contentTo").html("");
                    pageNo = 1;
                    queryList(pageNo,function(){},true); 
                } else {
                 $.toast(result.message, "cancel");
                }
             });
            
          }, function() {
            //取消操作
          });
        
    }
    /* global servicePay */
    $("#confirmBondPay").click(function(){
        var type = $("input[name='radioPay']:checked").val();
        var id = $("#payId").val();
        var payMoney = $("#payMoney").html();
        if (id == ''){
            $.toast("参数错误", "cancel");
            return;
        }
        if (type == 1) {
            passwordPay(id);
        } else if (type == 2){
            /* global isMiniProgram wx */
            if ( isMiniProgram () ) {//小程序就跳转小程序支付
                var params = {
                    type: 160,//支付类型
                    id: id,//关联id
                    payMoney: payMoney,//支付金额
                    content:'缴纳专场上架消保金',//订单说明
                    jumpUrl:'/pages/special_manage',//支付成功后跳转地址  为空返回当前页面 不可以带 ? 和 &
                };
                wx.miniProgram.navigateTo({
                    url: "/pages/pay/index?params=" + JSON.stringify(params)
                });
            } else {
                $.post('/antique/api/onlinePay/wxpay/create_wxpay', {
                    token:getCookie('token'),
                    type:160,
                    appSource:1,
                    keyId:id,
                    money:servicePay(payMoney),
                    content:'缴纳专场上架消保金' + payMoney
                }, function (result) {
                    var appId = result.data.appid;
                    var nonceStr = result.data.nonce_str;
                    var prepay_id = result.data.prepay_id;
                    var mch_id = result.data.mch_id;
                    /* global goWeiPay */
                    goWeiPay(appId,nonceStr,prepay_id,mch_id);
                });
            }
        }
    
    });
    /*global timeToTimestamp hex_md5*/
    function passwordPay (id) {
        /* global havePayPwd */
        havePayPwd (function(havePwd) {
            if (havePwd) {
                $.modal({
                    title: "请输入支付密码",
                    text: '<input class="weui-input weui-prompt-input" type="password" id="password"  placeholder="没有设置密码可不填" />',
                    buttons: [ 
                      { text: "取消", className: "default" },
                      { text: "确定", onClick: function(){
                        var password = $("#password").val();
                        var params = {};
                        params.id = id;
                        params.token = getCookie('token');
                        var time = timeToTimestamp(new Date());
                        if (password == '') {
                            params.payPass = hex_md5(time + '').toUpperCase(); 
                        } else {
                            var passwords = hex_md5(password).toUpperCase();
                            params.payPass = hex_md5(passwords + time).toUpperCase();
                        }
                        params.time = time;
                        publicSpecial(params);
                       } }
                      
                    ]
                  });
            } else {
                var password = '';
                var params = {};
                params.id = id;
                params.token = getCookie('token');
                var time = timeToTimestamp(new Date());
                if (password == '') {
                    params.payPass = hex_md5(time + '').toUpperCase(); 
                } else {
                    var passwords = hex_md5(password).toUpperCase();
                    params.payPass = hex_md5(passwords + time).toUpperCase();
                }
                params.time = time;
                publicSpecial(params);
            }
        })

        
        
    }

    function publicSpecial (params) {
        $.post('/antique/api/mallAuction/manage/publish_auction', params, function (result) {
            if (result.code == 0) {
                $.toast("操作成功！");
                $("#contentTo").html("");
                pageNo = 1;
                queryList(pageNo,function(){},true);
            } else {
                $.toast(result.message, "cancel");
            }
        });
               
    }

